package com.hou.edustatistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hou.commonutils.R;
import com.hou.edustatistics.client.UcenterClient;
import com.hou.edustatistics.entity.StatisticsDaily;
import com.hou.edustatistics.mapper.StatisticsDailyMapper;
import com.hou.edustatistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-14
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {

        // 删除之前该日期的数据
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("date_calculated", day);

        baseMapper.delete(queryWrapper);


        // 远程调用统计某一天的注册用户量
        R registerDay = ucenterClient.countRegisterDay(day);

        Integer countRegister = (Integer) registerDay.getData().get("countRegister");

        //把获取数据添加数据库，统计分析表里面
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister); //注册人数
        sta.setDateCalculated(day);//统计日期

        sta.setVideoViewNum(RandomUtils.nextInt(100, 200));
        sta.setLoginNum(RandomUtils.nextInt(100, 200));
        sta.setCourseNum(RandomUtils.nextInt(100, 200));
        baseMapper.insert(sta);

    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();

        queryWrapper.between("date_calculated", begin, end);
        queryWrapper.select("date_calculated", type);

        List<StatisticsDaily> staList = baseMapper.selectList(queryWrapper);

        // 封装为数据结构
        // list集合会编程json数组，对象会变成json集合

        ArrayList<String> dateList = new ArrayList<>();
        ArrayList<Integer> numList = new ArrayList<>();

        for (StatisticsDaily statisticsDaily : staList) {
            dateList.add(statisticsDaily.getDateCalculated());

            switch (type) {
                case "login_num":
                    numList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num":
                    numList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    numList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("numList",numList);

        return map;

    }
}
